package com.cloudptt.wp.ui.more;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.SwitchPreference;
import android.view.Window;
import android.widget.TextView;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.config.call.AnswerModeConfig;
import com.cloudptt.api.product.config.call.LocalConfig;
import com.cloudptt.api.product.config.msg.MsgConfig;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.McIdInfo;

public class FuncPreferenceActivity extends PreferenceActivity implements
        SharedPreferences.OnSharedPreferenceChangeListener {
    private SwitchPreference answerModeVoicePref = null;
    private SwitchPreference answerModeVideoPref = null;

    private SwitchPreference answerModeVoiceGroupPref = null;
    private SwitchPreference answerModeVideoGroupPref = null;

    private SwitchPreference msgReceiptRecvPref = null;
    private SwitchPreference msgReceiptReadPref = null;
    private SwitchPreference msgReceiptDownloadPref = null;

    private EditTextPreference saInfoPreference = null;

    private SharedPreferences sp = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_func);
        initPreference();
    }

    private void initPreference() {
        answerModeVoicePref = (SwitchPreference) findPreference(AnswerModeConfig.Key_AutoAnswerVoice);
        answerModeVideoPref = (SwitchPreference) findPreference(AnswerModeConfig.Key_AutoAnswerVideo);

        answerModeVoiceGroupPref = (SwitchPreference) findPreference(AnswerModeConfig.Key_AutoAnswerVoiceGroup);
        answerModeVideoGroupPref = (SwitchPreference) findPreference(AnswerModeConfig.Key_AutoAnswerVideoGroup);

        msgReceiptRecvPref = (SwitchPreference) findPreference(MsgConfig.Key_ReceiptRecv);
        msgReceiptReadPref = (SwitchPreference) findPreference(MsgConfig.Key_ReceiptRead);
        msgReceiptDownloadPref = (SwitchPreference) findPreference(MsgConfig.Key_ReceiptDownload);

        saInfoPreference = (EditTextPreference) findPreference("sa_info");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        sp = getPreferenceScreen()
                .getSharedPreferences();
        boolean answerModeVoice = sp.getBoolean(AnswerModeConfig.Key_AutoAnswerVoice, false);
        String val = getAnswerModeStr(answerModeVoice);
        answerModeVoicePref.setSummary(val);

        boolean answerModeVideo = sp.getBoolean(AnswerModeConfig.Key_AutoAnswerVideo, false);
        val = getAnswerModeStr(answerModeVideo);
        answerModeVideoPref.setSummary(val);

        boolean answerModeVoiceGroup = sp.getBoolean(AnswerModeConfig.Key_AutoAnswerVoiceGroup, true);
        Log.debug(this.getClass(),"onResume answerModeVoiceGroup = " + answerModeVoiceGroup);
       val = getAnswerModeStr(answerModeVoiceGroup);
        Log.debug(this.getClass(),"onResume val = " + val);
        answerModeVoiceGroupPref.setSummary(val);

        boolean answerModeVideoGroup = sp.getBoolean(AnswerModeConfig.Key_AutoAnswerVideoGroup, true);
        val = getAnswerModeStr(answerModeVideoGroup);
        answerModeVideoGroupPref.setSummary(val);

        boolean msgReceiptRecv = sp.getBoolean(MsgConfig.Key_ReceiptRecv, false);
        val = getCheckStr(msgReceiptRecv);
        msgReceiptRecvPref.setSummary(val);

        boolean msgReceiptRead = sp.getBoolean(MsgConfig.Key_ReceiptRead, false);
        val = getCheckStr(msgReceiptRead);
        msgReceiptReadPref.setSummary(val);

        boolean msgReceiptDownload = sp.getBoolean(MsgConfig.Key_ReceiptDownload, false);
        val = getCheckStr(msgReceiptDownload);
        msgReceiptDownloadPref.setSummary(val);

        String saInfo = sp.getString(LocalConfig.Key_SaInfo, LocalConfig.Defaule_SaInfo);
        saInfoPreference.setSummary(saInfo);


        sp.registerOnSharedPreferenceChangeListener(this);
    }

    private String getAnswerModeStr(boolean isCheck) {
        if (isCheck) {
            return getString(R.string.answer_mode_summary_on);
        }
        return getString(R.string.answer_mode_summary_off);
    }

    private String getCheckStr(boolean isCheck) {
        if (isCheck) {
            return getString(R.string.msg_receipt_summary_on);
        }
        return getString(R.string.msg_receipt_summary_off);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {
        SysApplication app = ((SysApplication) getApplicationContext());
        Product product = app.getProduct();
        SysConfig config = product.getSysConfig();

        if (key.equals(LocalConfig.Key_SaInfo)) {
            String saInfo = sp.getString(LocalConfig.Key_SaInfo, McIdInfo.Default_SaInfo);
            saInfoPreference.setSummary(saInfo);
            config.updateSaInfo(saInfo);
            product.setSaInfoForReportData(saInfo);
        }
        else if (key.equals(AnswerModeConfig.Key_AutoAnswerVoice)) {
            boolean answerModeVoice = sp.getBoolean(AnswerModeConfig.Key_AutoAnswerVoice, false);
            config.getAnswerModeConfig().setAutoAnswerVoice(answerModeVoice);
        }
        else if (key.equals(AnswerModeConfig.Key_AutoAnswerVideo)) {
            boolean answerModeVideo = sp.getBoolean(AnswerModeConfig.Key_AutoAnswerVideo, false);
            config.getAnswerModeConfig().setAutoAnswerVideo(answerModeVideo);
        }
        else if (key.equals(AnswerModeConfig.Key_AutoAnswerVoiceGroup)) {
            boolean answerModeVoice = sp.getBoolean(AnswerModeConfig.Key_AutoAnswerVoiceGroup, true);
            config.getAnswerModeConfig().setAutoAnswerVoiceGroup(answerModeVoice);
        }
        else if (key.equals(AnswerModeConfig.Key_AutoAnswerVideoGroup)) {
            boolean answerModeVideo = sp.getBoolean(AnswerModeConfig.Key_AutoAnswerVideoGroup, true);
            config.getAnswerModeConfig().setAutoAnswerVideoGroup(answerModeVideo);
        }
        else if (key.equals(MsgConfig.Key_ReceiptRecv)
                    || key.equals(MsgConfig.Key_ReceiptRead)
                    || key.equals(MsgConfig.Key_ReceiptDownload)) {
            boolean msgReceiptRecv = sp.getBoolean(MsgConfig.Key_ReceiptRecv, false);
            boolean msgReceiptRead = sp.getBoolean(MsgConfig.Key_ReceiptRead, false);
            boolean msgReceiptDownload = sp.getBoolean(MsgConfig.Key_ReceiptDownload, false);
            config.getMsgConfig().setReceiptRecv(msgReceiptRecv,msgReceiptRead,msgReceiptDownload);
        }
    }
}
